set(CMAKE_SYSTEM_NAME Linux)
set(SDK_INSTALL_PREFIX  /opt/sdks/rk3399)
set(CMAKE_C_FLAGS
    "--sysroot $ENV{OECORE_TARGET_SYSROOT} $ENV{CFLAGS}"
    CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS
    "--sysroot $ENV{OECORE_TARGET_SYSROOT} $ENV{CXXFLAGS}"
    CACHE STRING "" FORCE)
set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "" FORCE)
set(CMAKE_LDFLAGS_FLAGS ${CMAKE_CXX_FLAGS} CACHE STRING "" FORCE)

# set(CMAKE_SYSROOT $ENV{OECORE_TARGET_SYSROOT})
set(
  CMAKE_C_COMPILER
  ${SDK_INSTALL_PREFIX}/rk-xwayland/3.0.1/sysroots/x86_64-rksdk-linux/usr/bin/aarch64-rk-linux/aarch64-rk-linux-gcc
  CACHE STRING "" FORCE)
set(
  CMAKE_CXX_COMPILER
  ${SDK_INSTALL_PREFIX}/rk-xwayland/3.0.1/sysroots/x86_64-rksdk-linux/usr/bin/aarch64-rk-linux/aarch64-rk-linux-g++
  CACHE STRING "" FORCE)

set(CMAKE_FIND_ROOT_PATH $ENV{OECORE_TARGET_SYSROOT}
                         $ENV{OECORE_NATIVE_SYSROOT})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Set CMAKE_SYSTEM_PROCESSOR from the sysroot name (assuming processor-distro-
# os).
if($ENV{SDKTARGETSYSROOT} MATCHES "/sysroots/([a-zA-Z0-9_-]+)-.+-.+")
  set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_MATCH_1})
endif()

# Include the toolchain configuration subscripts
file(GLOB toolchain_config_files "${CMAKE_TOOLCHAIN_FILE}.d/*.cmake")
foreach(config ${toolchain_config_files})
  include(${config})
endforeach()
